//
//  HQCollectionViewController.m
//  Dsgfds
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 胡泉. All rights reserved.
//

#import "HQCollectionViewController.h"
#import "HQCircleLayout.h"
#import "HQCollectionViewCell.h"
#import "BeautifulViewController.h"
#import "ItemsViewController.h"
#import "Reachability.h"
@interface HQCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIImageView *endImageView;
@property (weak, nonatomic) IBOutlet UIImageView *textImageView;
@property (weak, nonatomic) IBOutlet UIImageView *titleImageView;
@property (weak, nonatomic) IBOutlet UICollectionView *gridView;
@property (nonatomic, assign) NSInteger cellCount;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) Reachability *reach;


@end

@implementation HQCollectionViewController

//- (void)dealloc {
//    
//    
//}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBarHidden = YES;
    HQCircleLayout *circleLayout = [[HQCircleLayout alloc] init];
    self.gridView.collectionViewLayout = circleLayout;
    self.gridView.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"return"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(returnLast)];
    _gridView.dataSource = self;
    _gridView.delegate = self;
    // 让scrollView不滚动
    self.automaticallyAdjustsScrollViewInsets = NO;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
//    [_gridView addGestureRecognizer:tap];
    UINib *nib = [UINib nibWithNibName:@"HQCollectionViewCell" bundle:nil];
     self.navigationItem.title = @"杂志";
    [self.gridView registerNib:nib forCellWithReuseIdentifier:@"cellId"];
    // 设置中心imageView
    UITapGestureRecognizer *tapCenter = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapCenter:)];
    [_textImageView addGestureRecognizer:tapCenter];
    
    _titleImageView.image = [UIImage imageNamed:@"bai"];
    _endImageView.image = [UIImage imageNamed:@"wei"];
    
    _textImageView.image = [UIImage imageNamed:@"meiwen.jpg"];
    _textImageView.layer.cornerRadius = 50;
    _textImageView.layer.masksToBounds = YES;
    _textImageView.contentMode = UIViewContentModeScaleToFill;
    _textImageView.userInteractionEnabled = YES;
    self.dataArray = @[@{@"image":@"meishi.jpg",@"id":@"9"},@{@"image":@"wenhua.jpg",@"id":@"11"},@{@"image":@"shishang.jpg",@"id":@"15"},@{@"image":@"kongjian.jpg",@"id":@"16"},@{@"image":@"qiche.jpg",@"id":@"18"},@{@"image":@"jianzhu.jpg",@"id":@"19"},@{@"image":@"shuma.jpg",@"id":@"20"},@{@"image":@"quwu.jpg",@"id":@"21"}];
    ///开启网络状况的监听
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    self.reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    [self.reach startNotifier]; //开始监听，会启动一个run loop

    
    // Do any additional setup after loading the view from its nib.
}

//通知
-(void)reachabilityChanged:(NSNotification*)note
{
    Reachability * reach = [note object];
    NSParameterAssert([reach isKindOfClass: [Reachability class]]);
    NetworkStatus status = [reach currentReachabilityStatus];
    
    //    //用于检测是否是WIFI
    //    NSLog(@"%d",([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable));
    //    //用于检查是否是3G
    //    NSLog(@"%d",([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable));
    
    if (status == NotReachable) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"当前网络不可用,请检查您的网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        NSLog(@"Notification Says Unreachable");
    }else if(status == ReachableViaWWAN){
        
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"您已连接移动网络,请考虑连接Wifi" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
    }else if(status == ReachableViaWiFi){
        
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"您已连接WIfi网络,请放心使用" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//        [alertView show];
        NSLog(@"连接上wifi");
    }
    // 删除通知对象
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}




- (void)returnLast
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    HQCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    cell.hqImageView.image = [UIImage imageNamed:_dataArray[indexPath.item][@"image"]];
    return cell;
}

- (void)handleTapCenter:(UITapGestureRecognizer *)sender
{
    BeautifulViewController *beauVC = [[BeautifulViewController alloc] initWithNibName:@"BeautifulViewController" bundle:nil];
    [self.navigationController pushViewController:beauVC animated:YES];
     
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ItemsViewController *itemVC = [[ItemsViewController alloc] init];

    itemVC.urlId = _dataArray[indexPath.item][@"id"];
    [self.navigationController pushViewController:itemVC animated:YES];
}

//- (void)handleTap:(UITapGestureRecognizer *)sender
//{
//    // 判断是否点击结束
//    if (sender.state == UIGestureRecognizerStateEnded) {
//    // 获取点击点的位置
//        CGPoint clickPoint = [sender locationInView:self.gridView];
//        NSIndexPath *indexPath = [self.gridView indexPathForItemAtPoint:clickPoint];
//        // 如果被点击的单元格存在
//        if (indexPath)
//        {
//            // 删除单元格
////            _cellCount --;
////            [self.gridView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
//            NSLog(@"1");
//        }
//        // 如果点击的空白处
//        else
//        {
////            _cellCount ++;
////            [self.gridView insertItemsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForItem:0 inSection:0]]];
//            NSLog(@"2");
//        }
//        
//    }
//}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
